#!/bin/bash
#
# 4shared.com module
# Copyright (c) 2010-2012 Plowshare team
#
# This file is part of Plowshare.
#
# Plowshare is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Plowshare is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Plowshare.  If not, see <http://www.gnu.org/licenses/>.

MODULE_PUTLOCKER_REGEXP_URL="http://\(www\.\)\?putlocker\.com/file/"

MODULE_PUTLOCKER_DOWNLOAD_OPTIONS=""
MODULE_PUTLOCKER_DOWNLOAD_RESUME=yes
MODULE_PUTLOCKER_DOWNLOAD_FINAL_LINK_NEEDS_COOKIE=yes

MODULE_PUTLOCKER_LIST_OPTIONS=""

putlocker_download() {

local COOKIEFILE=$1
local URL=$2

	Pageinit=$(curl "$URL") || return

	hash=$(echo "$Pageinit" | parse_attr 'name="hash"' 'value') 
	tryingsomethingcompletelydifferent=$(echo "$Pageinit" | grep -m1 '<h1>') 
	tryingsomethingcompletelydifferent1=$(echo "$tryingsomethingcompletelydifferent" | replace "<h1>" "" ) || return
	tryingsomethingcompletelydifferent1=$(echo "$tryingsomethingcompletelydifferent1" | replace "<strong>*</strong></h1>" "" ) || return
	
	Pageauth=$(curl -e $URL -d "hash=$hash&confirm=Continue+as+Free+User" "$URL") || return
# echo "$Pageauth" > pageauth
	getreq=$(echo "$Pageauth" | grep /get)
#	echo "$Pageauth" > pageauth
	getreq=${getreq:12}
	getreq=${getreq%?}
	getreq=${getreq%?}
	getreq=${getreq%?}
	baseurl=$(basename_url "$URL")
     	getreq="$baseurl$getreq"

	Pagexmllink=$(curl -e $URL "$getreq") || return
#	echo "$Pagexmllink" > pagexml
	finallink=$(echo "$Pagexmllink" | parse_all_attr 'media:content' 'url') # sed 's/http:\/\/media/\^http:\/\/media/g' | tr -s "^" "\n" | grep http| sed 's/\ .*//g' | grep http)
  
  # finallink=${finallink%?}
  finallink=$(echo "$finallink" | replace "'" "%27" )
  finallink=$(echo "$finallink" | replace "amp;" "" )
 # finallink=$(echo "$finallink" | replace "%2B" "+" )
 # finallink=$(echo "$finallink" | replace "%3D" "=" )
 
 curl -I "$finallink" > putfinallinkheader
# echo "$finallink" > putfl
 testlink=$(curl -I "$finallink" | grep 'Location: ') || return
if [[ $testlink != '' ]]; then
    FILE_URL=$(curl -I "$finallink" | grep Location | replace 'Location: ' '') || return
  else
    FILE_URL=$(echo "$finallink")
  fi
	FILENAME=$(echo "$tryingsomethingcompletelydifferent1" | strip)
	
	echo "$FILE_URL" 
	echo "$FILENAME" 
}
putlocker_upload() {
    local -r FILE=$2
    local -r DESTFILE=$3
    local -r BASE_URL='http://upload.putlocker.com/uploadapi.php'
    local DATA MSG USER PASSWORD DL_URL

    test "$AUTH_FREE" || return $ERR_LINK_NEED_PERMISSIONS
    split_auth "$AUTH_FREE" USER PASSWORD || return

    DATA=$(curl_with_log \
        -F "file=@$FILE" \
        -F "user=jack44556677" \
        -F "password=bullshit" \
        -F 'convert=0' \
        "$BASE_URL") || return

    MSG=$(parse_tag message <<< "$DATA") || return

    if match 'File Uploaded Successfully' "$MSG"; then
        DL_URL=$(parse_tag link <<< "$DATA") || return
        echo "$DL_URL"
        return 0
    elif match 'Wrong username or password' "$MSG"; then
        return $ERR_LOGIN_FAILED
    fi

    log_error "Unexpected status: $MSG"
    return $ERR_FATAL
}